import jieba
from matplotlib import pyplot as plt
from wordcloud import WordCloud
from PIL import Image
import numpy as np
from pymysql import *
from utils.query import *
import json

def get_address_img(targetImagSrc, resImgSrc, field=''):
    if field:
        sql = 'select tags from house_info'
        data = querys(sql, [], 'select')
        dataList = []
        for i in data:
            dataList.append(json.loads(i[0]))
        text = ''
        for item in dataList:
            for tag in item:
                text += tag
    else:
        # 获取数据表中的address数据
        sql = 'select address from house_info'
        data = querys(sql, [], 'select')
        # 将所有地址连接成一个字符串
        text = ''
        for item in data:
            # 将每一行连接起来
            text += item[0]

    # 分词
    cut = jieba.cut(text)
    string = ' '.join(cut)

    # 绘制词云图
    img = Image.open(targetImagSrc)
    img_arr = np.array(img)
    wc = WordCloud(
        background_color='white',
        mask=img_arr,
        font_path='./fonts/simhei.ttf'
        # font_path='./fonts/STHUPO.TTF'
    )
    wc.generate_from_text(string)

    plt.figure(2)
    plt.imshow(wc)
    plt.axis('off')

    # dpi -- 清晰度
    plt.savefig(resImgSrc, dpi=500)

    return resImgSrc

# test
# 获取房屋地址词云图
# get_address_img('./static/image/address.png', './static/image/address_cloud.jpg')
# 获取房屋公司词云图
# get_address_img('./static/image/company.png', './static/image/company_cloud.jpg')
# 获取房屋标签词云图
get_address_img('./static/image/tags.png', './static/image/tags_cloud.jpg', field='tags')